<template>
  <div class="logFund">
    <el-dialog :title="dialog.title" :visible.sync="dialog.show">
      <div class="form">
        <el-form :model="formData" ref="form" :rules="form_rules" label-width="120px">
          <!-- <el-form :model="formData" ref="form" label-width="120px"> -->
          <el-form-item prop="type" label="收支类型:">
            <el-select v-model="formData.type" placeholder="请选择类型">
              <el-option
                :label="formtype"
                :value="formtype"
                v-for="(formtype,index) in format_type_list"
                :key="index"
              >{{formtype}}</el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="describe" label="收支描述:">
            <el-input v-model="formData.describe"></el-input>
          </el-form-item>
          <el-form-item prop="income" label="收入:">
            <el-input v-model="formData.income"></el-input>
          </el-form-item>
          <el-form-item prop="expend" label="支出:">
            <el-input v-model="formData.expend"></el-input>
          </el-form-item>
          <el-form-item prop="cash" label="帐户现金:">
            <el-input v-model="formData.cash"></el-input>
          </el-form-item>
          <el-form-item prop="remark" label="备注:">
            <el-input v-model="formData.remark"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="dialog.show = false">取 消</el-button>
            <el-button type="primary" @click="onSubmit('form')">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "logfound",
  props: {
    dialog: Object,
    formData: Object
  },
  data() {
    return {
      format_type_list: ["提现", "充值", "转账"],
      form_rules: {
        describe: [
          {
            required: true,
            message: "收支描述不能为空",
            trigger: "blur"
          }
        ],
        income: [
          {
            required: true,
            message: "收入不能为空",
            trigger: "blur"
          }
        ],
        expend: [
          {
            required: true,
            message: "支出不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    onSubmit(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          // 判断表单值来自哪个页面,
          let url = this.dialog.option == "add" ? "add": `edit/${this.formData.id}`
          this.$axios.post(`/api/profile/${url}`, this.formData).then(res => {
            // 操作成功
            this.$message({
              message: "保存成功！",
              type: "success"
            });
            this.dialog.show = false;
            this.$emit("update");//在父级调用数据请求方法
          });
        }
      });
    }
  }
};
</script>

